<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>{% block title %}Welcome!{% endblock %}</title>
        {% block stylesheets %}{% endblock %}
        <style>
            .goods{
                width: 100px;
                border: 1px solid red;
                margin: 10px 20px 10px 20px;
                float: left;
            }
        </style>
    </head>
    <body>
        <h1>{{ goods.goods_name }}</h1>
        <h2>还有：<span id="goods_num">{{ goods.goods_num }}</span>件</h2>
        <button id="button" value="{{ goods.id }}">抢！抢！抢！</button>
    </body>
</html>
<script src="{{ asset('js/jquery.js') }}"></script>
<script>
    $(function () {
        $("#button").click(function () {
            var goods_id = $(this).attr("value");
            $.ajax({
                url: "{{ path('snap_up') }}",
                dataType: 'json',
                data: {'goods_id': goods_id},
                type: 'post',
                success: function (e) {
                    if (e.code == 0) {
                        $('#goods_num').html(e.data.goods_num);
                        alert("抢购成功，我们将尽快给您发货，抢购数量："+e.data.snap_num);
                    } else {
                        alert(e.message);
                    }
                }
            })
        });
    })
</script>
